package com.service.lotterySystem.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.lotterySystem.dto.AddressDto;
import com.service.lotterySystem.dto.JoinInfoDto;
import com.service.lotterySystem.dto.ReceiveAddressDto;
import com.service.lotterySystem.dto.UserDto;
import com.service.lotterySystem.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.service.config.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 19:15
 * @description：
 */
@RestController
@RequestMapping("addressController")
@Api(value = "收货地址接口", tags = "收货地址接口")
public class ReceiveAddressController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private AddressService addressService;

    /**
     * 上传收货地址
     * @param
     * @return
     */
    @PostMapping(value = "addAddress",produces="application/json")
    @ApiOperation(value = "上传收货地址")
    @ResponseBody
    public Result addAddress(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                              @RequestBody AddressDto addressDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                addressDto.setuId(userId);
                addressDto.setuName(userDto.getNickName());
                return addressService.addAddress(addressDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * 编辑收货地址
     * @param
     * @return
     */
    @PostMapping(value = "updateAddress",produces="application/json")
    @ApiOperation(value = "编辑收货地址")
    @ResponseBody
    public Result updateAddress(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                             @RequestBody AddressDto addressDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                UserDto userDto = JsonXMLUtils.json2obj(redisUtil.getValue(cloudAuthToken + userId), UserDto.class);
                addressDto.setuId(userDto.getId());
                return addressService.updateAddress(addressDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }
    /**
     * 删除收货地址
     * @param
     * @return
     */
    @GetMapping(value = "deleteAddress",produces="application/json")
    @ApiOperation(value = "删除收货地址")
    @ResponseBody
    public Result deleteAddress(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestParam @ApiParam("地址id") String addressId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return addressService.deleteById(addressId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * （默认只能选一个）
     * @param
     * @return
     */
    @GetMapping(value = "defaultAddress",produces="application/json")
    @ApiOperation(value = "选择收货地址（默认只能选一个）")
    @ResponseBody
    public Result defaultAddress(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                @RequestParam @ApiParam("地址id") String addressId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                return addressService.defaultAddress(userId,addressId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 查询用户所有收货地址
     * @param
     * @return
     */
    @GetMapping(value = "queryUserAddress",produces="application/json")
    @ApiOperation(value = "查询用户所有收货地址")
    @ResponseBody
    public Result queryUserAddress(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                List<ReceiveAddressDto> receiveAddressDtos = addressService.queryAddressByUid(userId);
                return Result.ok().setData(receiveAddressDtos);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }
 /*   *//**
     * 运营查询用户所有收货地址
     * @param
     * @return
     *//*
    @GetMapping(value = "queryDefaultAddress",produces="application/json")
    @ApiOperation(value = "运营查询用户所有收货地址")
    @ResponseBody
    public Result queryDefaultAddress(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                List<ReceiveAddressDto> receiveAddressDtos = addressService.queryAddressByUid(userId);
                return Result.ok().setData(receiveAddressDtos);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }*/
}
